package console

import (
	"fmt"

	tui "github.com/marcusolsson/tui-go"
)

type Tplte struct {
	Type    int    `json:"type"`
	Content string `json:"content"`
}

var (
	template []Tplte
)

func tplte_drawing() {

	title.SetText("选择一个合适的模板")

	template = Tplte_Get()

	library = tui.NewTable(0, 0)
	library.SetColumnStretch(0, 1)
	library.SetColumnStretch(1, 3)
	library.SetFocused(true)

	library.AppendRow(
		tui.NewLabel("插槽数量"),
		tui.NewLabel("模板内容"),
	)

	for _, v := range template {
		library.AppendRow(
			tui.NewLabel(fmt.Sprintf("%d", v.Type)),
			tui.NewLabel(v.Content),
		)
	}
	//status.SetText("按下F1获取帮助")
	//status.SetPermanentText(fmt.Sprintf("Page %d,PageSize %d", page, pagesize))

	root = tui.NewVBox(
		title,
		library,
		tui.NewSpacer(),
		progress,
		status,
	)
	ui.SetWidget(root)
	ui.Repaint()
	tplte_key()
}

func tplte_key() {

	// library.OnSelectionChanged(func(t *tui.Table) {
	// 	status.SetPermanentText(fmt.Sprintf("%d", t.Selected()))
	// })

	ui.ClearKeybindings()
	ui.SetKeybinding("Enter", insert_enterPress)
	ui.SetKeybinding("Esc", event_drawing)
	ui.SetKeybinding("F5", func() { ui.Repaint() })

}

func insert_enterPress() {
	op := library.Selected()

	event_insert()
	entry_name.SetText(template[op-1].Content)
}
